package com.syn.synold4s.repository;
import static com.syn.synold4s.constants.TagCode.codeDriver;

import com.gaoyi.basemodule.baseCallback.Callback;
import com.gaoyi.basemodule.baseCallback.OnResultListener;
import com.gaoyi.basemodule.baseUtil.GsonUtil;
import com.gaoyi.basemodule.baseUtil.OKHttpUtil;
import com.gaoyi.basemodule.baseUtil.SPUtil;
import com.gaoyi.basemodule.baseconstant.SPName;
import com.google.gson.reflect.TypeToken;
import com.syn.synold4s.bean.dto.GoogleMapBean;
import java.lang.reflect.Type;

public class GoogleMapRepository {
    public void deviceDetails(String tagCode, String id, OnResultListener onResultListener, Type typeofT) {
        String projCode = SPUtil.getInstance().getString(SPName.PROJ_CODE);
        OKHttpUtil.getInstance().getAsync(OKHttpUtil.NET_URL + "/api/Dashboards/Details?projCode=" + projCode + "&tagCode=" + tagCode + "&id=" + id, new Callback() {
            @Override
            public void onSuccess(Object obj) {
                onResultListener.onSuccess(obj);
            }

            @Override
            public void onFailed(Exception e) {
                onResultListener.onFails(e);
            }
        }, typeofT);
    }

    public void towerDetails(String id, String tagCode, OnResultListener onResultListener, Type typeofT){
        String api_str = "tcst";
        if (codeDriver.equals(tagCode)){
            api_str = "dsmst";
        }
        OKHttpUtil.getInstance().getAsync(OKHttpUtil.NET_URL + "/api/"+api_str+"/dashboard/details?id="+id, new Callback() {
            @Override
            public void onSuccess(Object obj) {
                onResultListener.onSuccess(obj);
            }

            @Override
            public void onFailed(Exception e) {
                onResultListener.onFails(e);
            }
        }, typeofT);
    }

    public GoogleMapBean getAddress(double lat,double lng, String language) {
        String result = OKHttpUtil.getInstance().getSync("https://maps.googleapis.com/maps/api/geocode/json?latlng=" + lat + "," +lng + "&language=" + language + "&key=AIzaSyAf_LvhnV6TDlfvp-iddWSFFsJb3RHGmyw");
        if (result == null) {
            return null;
        }
        try {
            GoogleMapBean apiResultData = (GoogleMapBean) GsonUtil.getInstance().fromJson(result, new TypeToken<GoogleMapBean>() {
            }.getType());
            return apiResultData;
        } catch (Exception e) {
            return null;
        }
    }
}
